import { BASE_URL } from './api.js';

const request = axios.create({
    baseURL: BASE_URL,
    method: 'get',
});

// 拦截器: interceptors
// 请求拦截
request.interceptors.request.use((conf) => {
    return {
        ...conf,
        headers: {
            Authorization: localStorage.getItem('token'),
        },
    };
});

// 响应拦截
request.interceptors.response.use(
    (res) => {
        layer.msg(res.data.message);
        return res;
    },
    (err) => {
        layer.msg('请求错误');
        // 跳转登录页
        if (err.response.status === 401) {
            window.parent.location.href = window.parent.location.origin + '/login.html';
        }
        return err;
    },
);

export default request;
